package bawei.com.bilibili;


import android.graphics.Color;

import android.os.Build;



import android.os.Handler;

import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;


import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;

import android.support.v4.app.Fragment;


import android.support.v4.app.FragmentActivity;

import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;

import android.support.v4.widget.DrawerLayout;

import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;



import android.support.v7.widget.Toolbar;

import android.view.MenuItem;


import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;


import java.util.ArrayList;
import java.util.List;



import bawei.com.adapter.MyTabadapter;

import bawei.com.fragment.FindFragment;

import bawei.com.fragment.RecommendFragment;

import bawei.com.fragment.SopCastFragment;


import bawei.com.fragment.To_themFragment;


import bawei.com.fragment.ZoneFragment;

public
class
MainActivity extends FragmentActivity implements View.OnClickListener{

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private List<Fragment> fragmentList = new ArrayList<>();
    private ViewPager viewPager;


    private DrawerLayout drawerLayout;
    private NavigationView navigationView;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = (DrawerLayout) findViewById(R.id.mDrawerLayout);
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        viewPager = (ViewPager) findViewById(R.id.view_page);
        //toolbar.setLogo(R.drawable.ico_user_default);
        //toolbar.setTitle("未登录");//设置主标题
        //toolbar.setNavigationIcon(R.drawable.ic_navigation_drawer);
       // toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));//设置主标题颜色
       // toolbar.setTitleTextAppearance(this, R.style.Theme_ToolBar_Base_Title);
        toolbar.inflateMenu(R.menu.nav_menu);//设置右上角的填充菜单
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int menuItemId = item.getItemId();
                if (menuItemId == R.id.a) {
                    Toast.makeText(MainActivity.this, "菜单一", Toast.LENGTH_SHORT).show();

                } else if (menuItemId == R.id.b) {
                    Toast.makeText(MainActivity.this, "菜单二", Toast.LENGTH_SHORT).show();

                } else if (menuItemId == R.id.action_item1) {
                    Toast.makeText(MainActivity.this, "菜单三", Toast.LENGTH_SHORT).show();

                } else if (menuItemId == R.id.action_item2) {
                    Toast.makeText(MainActivity.this, "菜单四", Toast.LENGTH_SHORT).show();

                }
                return true;
            }
        });//1111111111
        //ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
        //上面的监听被下面的就监听给取代了，由于回报空指针异常，google推出了addDrawerListener()，
        // 对它做了空指针的判断
        //drawerLayout.addDrawerListener(toggle);
        //该方法会自动和actionBar关联, 将开关的图片显示在了action上，如果不设置，也可以有抽屉的效果，不过是默认的图标
        //toggle.syncState();
        //为侧边栏的每一个子item设置监听
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {


                //找到MenuItem的id
                int id = item.getItemId();
                switch (id) {
                    case R.id.nav_home:
                        Toast.makeText(MainActivity.this,"nav_home",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.nav_favorite:
                        Toast.makeText(MainActivity.this,"nav_favorite",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.nav_followers:
                        Toast.makeText(MainActivity.this,"nav_followers",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.nav_settings:
                        Toast.makeText(MainActivity.this,"nav_settings",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.nav_style:
                        Toast.makeText(MainActivity.this,"nav_style",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.night:
                        Toast.makeText(MainActivity.this,"night",Toast.LENGTH_SHORT).show();
                        break;
                }
                drawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }
        });
        adda();
        MyTabadapter myTabadapter = new MyTabadapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(myTabadapter);
        viewPager.setOffscreenPageLimit(5);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabsFromPagerAdapter(myTabadapter);
        tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
        tabLayout.setTabTextColors(Color.GRAY, Color.WHITE);
        findViewById(R.id.image2).setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(navigationView);
            }
        });
    }

    public void adda() {

        fragmentList.add(new SopCastFragment());
        fragmentList.add(new RecommendFragment());
        fragmentList.add(new To_themFragment());
        fragmentList.add(new ZoneFragment());
        fragmentList.add(new FindFragment());

    }

    //按物理返回键的时候，收缩侧边栏
    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onClick(View v) {

    }
}